package com.cgtong.android.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;

import com.cgtong.android.R;
import com.cgtong.android.adapter.MessageAdapter;
import com.cgtong.android.base.BaseTitleActivity;
import com.cgtong.android.bean.MessageBean;
import com.cgtong.android.callback.EmptyCallback;
import com.cgtong.android.mvp.contract.GetMessageContract;
import com.cgtong.android.mvp.presenter.GetMessagePresenter;
import com.cgtong.android.util.PostUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.kingja.loadsir.callback.SuccessCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 消息列表
 */
public class MyMessageListActivity extends BaseTitleActivity implements GetMessageContract.View {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout mSwipeLayout;

    private List<MessageBean> mList = new ArrayList<>();
    private BaseQuickAdapter<MessageBean, BaseViewHolder> mAdapter;
    private int msgType;
    private GetMessagePresenter messagePresenter;

    public static void start2Activity(Context context, String title, int msgType){
        Intent intent = new Intent(context, MyMessageListActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("msgType", msgType);
        context.startActivity(intent);
    }

    @Override
    protected String getContentTitle() {
        return getIntent().getStringExtra("title");
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_message_list;
    }

    @Override
    protected void loadDataByNet() {
        msgType = getIntent().getIntExtra("msgType", 0);
        messagePresenter = new GetMessagePresenter(this);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        mList.clear();
        initData();
    }

    @Override
    protected void onDataReloadByNet(View v) {
    }

    private void initView() {
        mSwipeLayout.setColorSchemeResources(R.color.color_scheme);
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                        mList.clear();
                        initData();
//                        mAdapter.notifyDataSetChanged();
                        mSwipeLayout.setRefreshing(false);
                    }
                }, 2000);
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MessageAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                MessageBean msg = mAdapter.getItem(position);
                MyMessageDetailActivity.start2Activity(MyMessageListActivity.this, msg);
            }
        });
    }

    private void initData() {
        messagePresenter.getMsgList(msgType);
    }

    @Override
    public void setDatas(List<MessageBean> list) {
        PostUtil.postCallbackDelayed(loadService, SuccessCallback.class);
        mList.clear();
        mAdapter.addData(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void setNullData() {
        PostUtil.postCallbackDelayed(loadService, EmptyCallback.class);
    }
}
